package com.eastfair.config.controller.entitycontroller;

import com.eastfair.config.entity.Switch;
import com.eastfair.config.service.entityservice.SwitchService;
import com.eastfair.core.base.R;
import com.eastfair.projectcore.vo.ConfigAttributeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 开关控制类
 * <p>
 * Date:2021/8/7
 * Time:15:37
 *
 * @author zjn
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/switch")
@Api(value = "开关控制类", tags = "开关控制类")
public class SwitchController {

    @Autowired
    private SwitchService switchService;

    /**
     * 获取开关属性信息
     *
     * @return
     */
    @GetMapping("/getAttributes")
    @ApiOperation(value = "获取开关属性信息", notes = "获取开关属性信息")
    public R<List<ConfigAttributeVO>> getSwitchAttributes() {
        return R.success(switchService.getAttributes());
    }

    /**
     * 获取开关列表
     *
     * @param sw 开关列表过滤条件
     * @return
     */
    @PostMapping("/getList")
    @ApiOperation(value = "获取开关列表", notes = "获取开关列表")
    public R<List<Switch>> getList(@RequestBody(required = false) Switch sw) {
        return R.success(switchService.getList(sw));
    }

    /**
     * 获取开关
     *
     * @param flagId 标识id
     * @return
     */
    @GetMapping("/get/{flagId}")
    @ApiOperation(value = "获取开关", notes = "获取开关")
    public R<Switch> getSwitch(@PathVariable("flagId") Long flagId) {
        return switchService.get(flagId);
    }

    /**
     * 根据条件获取单个开关
     * 必填参数：
     * subjectType 使用主体
     * languageType 语言环境
     * keyWord 关键字
     *
     * @param sw 开关过滤条件
     * @return 开关实体
     */
    @PostMapping("/getByEntity")
    @ApiOperation(value = "根据条件获取单个开关", notes = "根据条件获取单个开关")
    public R<Switch> getByEntity(@RequestBody @Validated({Switch.VerifyGetByEntity.class}) Switch sw) {
        return switchService.getByEntity(sw);
    }

    /**
     * 新增开关
     *
     * @param sw 开关实体
     * @return 是否成功
     */
    @PostMapping("/saveEntity")
    @ApiOperation(value = "新增开关", notes = "新增开关")
    public R<Boolean> saveEntity(@RequestBody Switch sw) {
        return switchService.saveEntity(sw);
    }

    /**
     * 删除开关
     *
     * @param flagId 开关标识id
     * @return 是否成功
     */
    @DeleteMapping("/{flagId}")
    @ApiOperation(value = "删除开关", notes = "删除开关")
    public R<Boolean> deleteEntity(@PathVariable("flagId") Long flagId) {
        return switchService.deleteEntity(flagId);
    }

    /**
     * 修改开关信息
     *
     * @param sw 开关信息
     * @return 是否成功
     */
    @PutMapping("/updateEntity")
    @ApiOperation(value = "修改开关信息", notes = "修改开关信息")
    public R<Boolean> updateEntity(@RequestBody @Validated({Switch.VerifyUpdateByEntity.class}) Switch sw) {
        return switchService.updateEntity(sw);
    }

}
